package com.designoutput.ui.toolGroup
{
	import com.designoutput.event.CardEditorEvent;
	import com.designoutput.event.NumberEvent;
	import com.designoutput.event.SliderEvent;
	import com.designoutput.icon.ZoomMinusOver;
	import com.designoutput.icon.ZoomPlusOver;
	import com.designoutput.ui.CardFaceEditor;
	import com.designoutput.ui.CardWindow;
	import com.designoutput.ui.controls.BarSplit;
	import com.designoutput.ui.controls.IconButton;
	import com.designoutput.ui.controls.Literal;
	import com.designoutput.ui.controls.NumberInput;
	import com.designoutput.ui.newControls.Slider;
	import com.designoutput.utils.Globals;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.utils.clearInterval;
	import flash.utils.setInterval;
	
	public class ZoomGroup extends Sprite
	{
		private var _cardWindow:CardWindow;
		private var _layerEditor:CardFaceEditor;
		
		private var _slider:Slider;
		private var _zoomNumberInput:NumberInput;
		private var _minusButton:IconButton;
		private var _plusButton:IconButton;
		
		private var _intervalId:uint;
		private var _amt:int = 0;
		
		public function ZoomGroup()
		{
			createChildren();
			
			_cardWindow = Globals.designoutputCard.canvas;
			setLayerEditor(_cardWindow.activeEditor);
			_cardWindow.addEventListener( CardEditorEvent.CURRENT_EDITOR_CHANGED, cardWindow_currentEditorChanged, false, 0, true);
		}
		
		private function createChildren():void{
			var split:BarSplit = new BarSplit();
			split.x = 0;
			split.y = 5;
			addChild(split);
			
			_minusButton = new IconButton(new ZoomMinusOver(0, 0));
			_minusButton.addEventListener(MouseEvent.MOUSE_DOWN, minusButton_mouseDownHandler, false, 0, true);
			_minusButton.x = 10;
			_minusButton.y = 10;
			addChild(_minusButton);
			
			_slider = new Slider(100, 100, 5, 800);
			_slider.addEventListener(SliderEvent.DRAG, slider_dragHandler, false, 0, true);
			_slider.x = 28;
			_slider.y = 4;
			addChild(_slider);
			
			_plusButton = new IconButton(new ZoomPlusOver(0, 0));
			_plusButton.addEventListener(MouseEvent.MOUSE_DOWN, plusButton_mouseDownHandler, false, 0, true);
			_plusButton.x = 133;
			_plusButton.y = 10;
			addChild(_plusButton);
			
			_zoomNumberInput = new NumberInput(36, 100, 5, 800, false, true);
			_zoomNumberInput.x = 155;
			_zoomNumberInput.y = 6;
			_zoomNumberInput.addEventListener(NumberEvent.VALUE_CHANGE, zoomNumberInput_valueChangeHandler, false, 0, true);
			addChild(_zoomNumberInput);
			
			var li:Literal = new Literal("%");
			li.x = 192;
			li.y = 6;
			addChild(li);
		}
		
		private function cardWindow_currentEditorChanged(e:CardEditorEvent):void {
			setLayerEditor(_cardWindow.activeEditor);
		}
		
		private function setLayerEditor(layer:CardFaceEditor):void {
			if(_layerEditor != null){
				_layerEditor.removeEventListener( CardEditorEvent.ZOOM_CHANGED, layer_zoomChangedHandler, false);
			}
			
			_layerEditor = layer;
			
			if(_layerEditor != null){
				_layerEditor.addEventListener( CardEditorEvent.ZOOM_CHANGED, layer_zoomChangedHandler, false, 0, true);
				layer_zoomChangedHandler(null);
			}
		}
		
		private function layer_zoomChangedHandler(e:Event):void {
			var percentZoom:int = _layerEditor.zoomlevel * 100;
			
			_zoomNumberInput.text = percentZoom.toString();
			
			_slider.setValue(percentZoom);
		}
		
		public function minusButton_mouseDownHandler(event:MouseEvent) : void
		{
			clearInterval(_intervalId);
			_intervalId = setInterval(animateZoomOut, 10);
		}
		
		public function plusButton_mouseDownHandler(event:MouseEvent) : void
		{
			clearInterval(_intervalId);
			_intervalId = setInterval(animateZoomIn, 10);
		}
		
		private function slider_dragHandler(evt:SliderEvent) : void
		{
			if (!evt.manual)
				setZoomFromSlider();
		}
		
		public function zoomNumberInput_valueChangeHandler(evt:NumberEvent) : void
		{
			_layerEditor.zoom(evt.value);
		}
		
		private function animateZoomOut() : void
		{
			_slider.setValue(_slider.getValue() - 10);
			setZoomFromSlider();
			if (_amt++ == 4)
			{
				clearInterval(_intervalId);
				_amt = 0;
			}
		}
		
		private function animateZoomIn() : void
		{
			_slider.setValue(_slider.getValue() + 10);
			setZoomFromSlider();
			if (_amt++ == 4)
			{
				clearInterval(_intervalId);
				_amt = 0;
			}
		}
		
		private function setZoomFromSlider() : void
		{
			var percentNum:Number = Math.round(_slider.getValue());

			_zoomNumberInput.text = Math.round(percentNum).toString();
			_layerEditor.zoom(percentNum);
			
			Globals.designoutputCard.resetTool();
		}
	}
}